import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

//登陆界面
public class Login extends JFrame implements ActionListener {
    JLabel t1, t2;
    JTextField n;
    JPasswordField p;
    JRadioButton r1, r2, r3;
    JButton b;
    String user = "", sql = "";
    DBAccess db = new DBAccess("SMSystem", "", "");
    CheckEmpty ckt;//判断输入是否为空

    public Login() {
        t1 = new JLabel("学号/工号：");
        t2 = new JLabel("密码：");
        n = new JTextField(20);
        p = new JPasswordField(8);
        r1 = new JRadioButton("学生", true);
        r2 = new JRadioButton("任课教师", false);
        r3 = new JRadioButton("教务处", false);
        b = new JButton("登陆");
        setLayout(null);

        p.setEchoChar('*');//这里设置符号 使密码不可见
        //设置标签大小和所在位置
        t1.setBounds(40, 50, 120, 50);
        n.setBounds(150, 50, 100, 38);
        t2.setBounds(280, 50, 80, 50);
        p.setBounds(340, 50, 80, 38);
        r1.setBounds(80, 100, 60, 60);
        r2.setBounds(140, 100, 90, 60);
        r3.setBounds(230, 100, 80, 60);
        b.setBounds(350, 110, 80, 35);
        //设置标签内的字体
        n.setFont(new Font("楷体", 1, 15));//字体大小
        p.setFont(new Font("楷体", 1, 15));//字体大小
        t1.setFont(new Font("楷体", 1, 20));//字体大小
        t2.setFont(new Font("楷体", 1, 20));//字体大小
        r1.setFont(new Font("楷体", 1, 15));//字体大小
        r2.setFont(new Font("楷体", 1, 15));//字体大小
        r3.setFont(new Font("楷体", 1, 15));//字体大小
        b.setFont(new Font("楷体", 1, 20));//字体大小

        t1.setForeground(Color.BLUE);//字体颜色
        t2.setForeground(Color.BLUE);//字体颜色
        r1.setForeground(Color.BLUE);//字体颜色
        r2.setForeground(Color.BLUE);//字体颜色
        r3.setForeground(Color.BLUE);//字体颜色
        b.setForeground(Color.BLUE);//字体颜色
        add(t1);
        add(t2);
        add(n);
        add(p);
        add(r1);
        add(r2);
        add(r3);
        add(b);
        b.addActionListener(this);
        setSize(500, 300);
        //以下的代码是让界面居中
        Dimension dim = getToolkit().getScreenSize();
        Rectangle abounds = getBounds();
        setLocation((dim.width - abounds.width) / 2,
                (dim.height - abounds.height) / 2);
        setVisible(true);
        requestFocus();
    }

    //获取用户名和验证密码进行登录
    int matchUser(String s, String n, String p) {
        String id[], pwd[];
        int i = 0, j = 0, a = 0;      //找到相同用户名和密码a=1
        db.dbcon();
        sql = "SELECT * FROM " + s;
        db.dbSelect(sql);
        try {
            while (db.rs.next()) {
                j++;
            }
            id = new String[j];
            pwd = new String[j];
            db.rs.beforeFirst();
            for (i = 0; i < j; i++) {
                db.rs.next();
                id[i] = db.rs.getString(2);
                pwd[i] = db.rs.getString(4);
            }
            for (i = 0; i < j; i++) {
                if (n.equals(id[i]) && p.equals(pwd[i])) {
                    return a = 1;
                }
            }

            db.dbclose();
        } catch (SQLException sqle) {
            System.out.println(1 + sqle.toString());
        } catch (Exception e) {
            System.out.println(2 + e.toString());
        }
        return a;
    }

    public void actionPerformed(ActionEvent e) {
        ckt = new CheckEmpty(n, p);
        //如果单选按钮是选中的
        if (r1.isSelected()) {
            user = r1.getText();     //用户身份设为单选按钮文本
        }
        //如果单选按钮是选中的
        else if (r2.isSelected()) {
            user = r2.getText();     //用户身份设为单选按钮文本
        }
        //如果单选按钮是选中的
        else if (r3.isSelected()) {
            user = r3.getText();     //用户身份设为单选按钮文本
        } else {
            JOptionPane.showMessageDialog(null, "请选择登陆身份！", "友情提示", 1);
            user = "";
        }
        if (e.getSource() == b) {
            if (ckt.check(0) != false) {
                if (user == "学生") {
                    if (matchUser("student", n.getText(), p.getText()) == 1) {
                        dispose();//setVisible(false) 清除界面
                        new SDo("student", n.getText());
                        JOptionPane.showMessageDialog(null, "成功登陆！", "友情提示", 1);
                        //setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(null, "学号/工号/密码不正确！\n请重新输入！", "友情提示", 1);
                    }
                } else if (user == "任课教师") {
                    if (matchUser("teacher", n.getText(), p.getText()) == 1) {
                        dispose();//setVisible(false);
                        new TDo("teacher", n.getText());
                        JOptionPane.showMessageDialog(null, "成功登陆！", "友情提示", 1);
                    } else {
                        JOptionPane.showMessageDialog(null, "学号/工号/密码不正确！\n请重新输入！", "友情提示", 1);
                    }
                } else if (user == "教务处") {
                    if (matchUser("teacher", n.getText(), p.getText()) == 1) {
                        dispose();//setVisible(false);
                        new DDo("teacher", n.getText());
                        JOptionPane.showMessageDialog(null, "成功登陆！", "友情提示", 1);
                    } else {
                        JOptionPane.showMessageDialog(null, "学号/工号/密码不正确！\n请重新输入！", "友情提示", 1);
                    }
                }
            }
        } else {
            System.exit(0);
        }
    }
}
